import 'dart:async';
import 'dart:convert' show json;

import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import '../demo/data_source/post.dart';
class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        actions: <Widget>[
          IconSource("Animation","22"),
        ],
      ),
      body:AnimationDemo(),
    );
  }
}

class AnimationDemo extends StatefulWidget {
  @override
  _AnimationDemoState createState() => _AnimationDemoState();
}

class _AnimationDemoState extends State<AnimationDemo> with TickerProviderStateMixin {
  AnimationController animationController;
  Animation animation;
  Animation animationColor;
  CurvedAnimation curvedAnimation;
  @override
  void initState() {
    super.initState();
    animationController=AnimationController(
      duration: Duration(milliseconds: 2000),
      vsync: this,
    );
    curvedAnimation=CurvedAnimation(parent: animationController, curve: Curves.bounceInOut);
    animation=Tween(begin: 30.0,end: 100.0).animate(curvedAnimation);
    animationColor=ColorTween(begin: Colors.red,end: Colors.red[900]).animate(curvedAnimation);
//    animationController.addListener((){
//      setState(() {
//      });
//    });
    animationController.addStatusListener((AnimationStatus status){
      print(status);
    });
  }

  @override
  void dispose() {
    super.dispose();
    animationController.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return AnimationHeartDemo(animationController: animationController,animations: [animation,animationColor],);
  }
}
class AnimationHeartDemo extends AnimatedWidget{
  final List<Animation> animations;
  final AnimationController animationController;
  const AnimationHeartDemo({this.animationController,this.animations}):super(listenable:animationController);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: IconButton(
          icon: Icon(Icons.favorite),
          iconSize: animations[0].value,
          color: animations[1].value,
          onPressed: (){
            switch(animationController.status){
              case AnimationStatus.completed :
                animationController.reverse();
                break;
              default:
                animationController.forward();
            }
          }
      ),
    );
  }

}

